//
// Created by grg on 2020/8/4.
// Copyright (c) 2020 龚任根. All rights reserved.
//

#ifndef TEST_CPP_PROJECT_VIEW_H
#define TEST_CPP_PROJECT_VIEW_H

#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include "GameWindowConfig.h"

class View {

protected:
    SDL_Rect *srcRect = nullptr;
    SDL_Rect *targetRect = nullptr;
    SDL_Surface *sdlSurface = nullptr;
    SDL_Texture *sdlTexture = nullptr;
    int z;


public:
    View(SDL_Rect *initMyRect, SDL_Rect *parentRect, int targetZ)
            : srcRect(initMyRect), targetRect(parentRect), z(targetZ) {
        sdlSurface = createSurface();
        sdlTexture = createTexture(sdlSurface);
    }

    View(int targetZ) : View(nullptr, nullptr, targetZ){};

    ~View() {
        SDL_DestroyTexture(sdlTexture);
        SDL_FreeSurface(sdlSurface);
        delete srcRect;
        delete targetRect;
    }

    void draw() {
        updateUI(sdlTexture, srcRect, targetRect);
    }

protected:
    virtual SDL_Surface *createSurface() = 0;

    virtual SDL_Texture *createTexture(SDL_Surface *surface) = 0;

public:

    virtual void onEvent(const SDL_Event *event) = 0;

    virtual void onPreUpdate() = 0;

    virtual void kill() = 0;

    void updateUI(SDL_Texture *imgTexture, SDL_Rect *srcRect, SDL_Rect *desRect) {
        SDL_RenderCopy(gContext->getRender(), imgTexture, srcRect, desRect);
    }
};


#endif //TEST_CPP_PROJECT_VIEW_H
